﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace TownsAndCountries
{
    class CountryView: ObservableCollection<City> , INotifyPropertyChanged
    {
        private List<Country> countries;
        private int current;

        public CountryView()
        {
            DataClassesDataContext ctx = new DataClassesDataContext();
            countries = ctx.Countries.ToList();
            current = 0;
        }

        public Country Current
        {
            get
            {
                if (current >= 0 && current < countries.Count)
                    return countries[current];
                else
                    return null;
            }
        }

        public String Cities
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach(var city in Current.Cities)
                {
                    sb.Append(city.CityName +",");
                }
                return sb.ToString();
            }
        }

        public void MoveNextCity()
        {
            CollectionViewSource.GetDefaultView(Current.Cities).MoveCurrentToNext();
            PropertyChanged(this, new PropertyChangedEventArgs("CurrentCity"));
        }

        public void MovePrevCity()
        {
            CollectionViewSource.GetDefaultView(Current.Cities).MoveCurrentToPrevious();
            PropertyChanged(this, new PropertyChangedEventArgs("CurrentCity"));
        }

        public City CurrentCity
        {
            get
            {
                return CollectionViewSource.GetDefaultView(Current.Cities).CurrentItem as City;
            }
        }

        public void MoveNext()
        {
            if (current < countries.Count - 1)
                current++;
            if (PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs("Current"));
                PropertyChanged(this, new PropertyChangedEventArgs("CurrentCity"));
            }
        }

        public void MovePrevious()
        {
            if (current > 0)
                current--;
            if (PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs("Current"));
                PropertyChanged(this, new PropertyChangedEventArgs("CurrentCity"));
            }
        }



        public event PropertyChangedEventHandler PropertyChanged;
    }
}
